package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Concepto;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class ConceptoDao implements Serializable {

	private static final long serialVersionUID = -4463197976026705004L;
	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public ConceptoDao() {
		super();
	}

	public ConceptoDao(Session session, SessionFactory sessionFactory) {
		super();
		this.session = session;
	}

	public List<Concepto> obtenerConceptos() {
		session = sessionFactory.openSession();

		Query query = session.createQuery("SELECT c FROM Concepto c");
		List<Concepto> c = query.list();

		session.close();

		return c;
	}

	public Concepto obtenerConceptoById(int id) {
		session = sessionFactory.openSession();
		Query query = session
				.createQuery("from Concepto where IdConcepto = :id ");
		query.setParameter("id", id);

		List<?> list = query.list();

		Concepto c = (Concepto) list.get(0);
		return c;
	}

	public void guardarConcepto(Concepto c) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(c);
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarConcepto(Concepto c) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(c);
		session.getTransaction().commit();
		session.close();
	}

}
